package com.cosina.game.crystallight.ui.battle.drawable;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;

import com.cosina.game.crystallight.engine.Architect;
import com.cosina.game.crystallight.engine.BattleEngine;
import com.cosina.game.crystallight.engine.util.CycleCount;
import com.cosina.game.crystallight.engine.util.UpCount;
import com.cosina.game.crystallight.model.FlyingStuff;
import com.cosina.game.crystallight.model.battle.FrameObject;
import com.cosina.game.crystallight.model.crystal.Crystal;
import com.cosina.game.crystallight.model.crystal.CrystalContainer;
import com.cosina.game.crystallight.model.crystal.Palm;
import com.cosina.game.crystallight.model.crystal.Tower;
import com.cosina.game.crystallight.model.enemy.Ps;
import com.cosina.game.crystallight.ui.battle.BattleResource;
import com.cosina.game.crystallight.ui.battle.drawadapter.BaseDrawable;
import com.cosina.game.crystallight.ui.common.PaintSuite;

public class AnimationDrawable extends BaseDrawable {
	private CycleCount vortexCycle = new CycleCount(5);
	
	@Override
	public void draw(Canvas canvas) {
		drawVortex(canvas);
		drawAttackRange(canvas);
		drawNewWaveFrame(canvas);
		drawPses(canvas);
		drawFrameObjects(canvas);
		drawFlyingStuff(canvas);
	}
	
	
	private void drawFlyingStuff(Canvas canvas) {
		for(FlyingStuff fs : BattleEngine.flyingManager.stuffs){
			String text = fs.score + "";
			char[] diagtals = text.toCharArray();
			int diagtalsCount = diagtals.length;
			for(int i = diagtalsCount -1; i>=0; i--){
				for(int j=0; j<10; j++){
					if(foo[j] == diagtals[i]){
						canvas.drawBitmap(BattleResource.digital[j], fs.currentLocation.x - (diagtalsCount - i) * 5,fs.currentLocation.y, PaintSuite.p4Simple);
					}
				}
			}
		}
	}
	private char[]  foo  = {'0','1','2','3','4','5','6','7','8','9'};

	private void drawFrameObjects(Canvas canvas) {
		for(FrameObject each : BattleEngine.psManager.frameObjects){
			canvas.drawBitmap(each.getFrame(), 
					5 + each.center.x - each.xspan, 
					5 + each.center.y - each.yspan, 
					PaintSuite.p4Simple);
		}
	}

	private void drawPses(Canvas canvas) {
		for(Ps each : BattleEngine.psManager.pses){
			canvas.drawBitmap(BattleResource.shot[each.color], 5 + each.location.x - 7, 5 + each.location.y - 7, PaintSuite.p4Simple);
		}
	}


	private void drawNewWaveFrame(Canvas canvas) {
		UpCount cc = BattleEngine.wavePanelManager.newWaveFrameIndex;
		if(null == cc)
			return;
		
		Point enemyHomeTop = BattleEngine.field.getEnemyHomeArea()[0];
		canvas.drawBitmap(BattleResource.newWaveFrames[cc.value], 5, 5 + enemyHomeTop.y *44 + 22, PaintSuite.p4Simple);
	}


	private Paint p4Select = new Paint();
	{
		p4Select.setColor(Color.GREEN);
		p4Select.setStyle(Style.STROKE);
		
	}
	private void drawAttackRange(Canvas canvas) {
		CrystalContainer container = Palm.getContainer();
		if(null == container || container.isEmpty())
			return;
		if(container instanceof Tower){
			Tower tower = (Tower)container;
			Crystal crystal = (Crystal)container.get();
			canvas.drawCircle(tower.center.x, tower.center.y, crystal.weapon.range, PaintSuite.paintForRangeCircle);
		}
	}

	private void drawVortex(Canvas canvas) {
		vortexCycle.doCycle();
		Architect builderManager = BattleEngine.architect;
		for(Point point : builderManager.getVortexLocations()){
			canvas.drawBitmap(BattleResource.vortex[vortexCycle.value], point.x * 44 + 5, point.y *44 + 5, PaintSuite.p4Simple);
		}
	}
}
